/*
*	ID : pallab81
* 	PROG : 11838
* 	LANG : C++
* 	2011-10-02-15.06.49 Sunday
*
* 	"I have not failed, I have just found 10000 ways that won't work.
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define foR(i,st,ed) for(int i = st ; i < ed ; ++i )
#define fo(i,ed) foR( i , 0 , ed )
#define foE(i,st,ed) for(int i = st ; i <= ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )

inline void wait(double seconds) {
    double endtime = clock()+(seconds * CLOCKS_PER_SEC);
    while (clock() < endtime) {
        ;
    }
}

int N;
int E;

vector< VI > path;
int matrix[ 2010 ][ 2010 ];

inline void _Set() {
    path.assign( N+5, VI() );
    SET(matrix, -1 );
}

inline void Read() {
    _Set();
    int u,v,d;
    fo(i,E) {
        scanf("%d %d %d",&u,&v,&d);
        path[u].pb(v);

        if( d==2 ) {
            path[v].pb( u );
        }
    }
}

inline void bfs(int node){
    queue< int > q;
    q.push( node );
    matrix[ node ][ node ]=1;
    while( q.empty()==false ){
        int father = q.front();
        q.pop();

        VI cango = path[father];
        for( int i=0,j=SZ(cango); i<j; ++i ){
            if( matrix[node][ cango[i] ]==-1 ){
                q.push( cango[i] );
                matrix[node][ cango[i] ]=1;
            }
        }
    }
}
inline void Proc() {
    for(int i=1;i<=N;++i ){
        bfs( i );
    }

    foE(i,1,N){
        foE(j,1,N){
            if( matrix[i][j]==-1 ){
                cout<<0<<"\n";
                return ;
            }

            //cout<<matrix[i][j]<<" ";
        }
        //cout<<"\n";
    }
    cout<<1<<"\n";
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in", "rt", stdin);
#endif

    while( scanf("%d %d",&N,&E)==2 ) {
        if( N==0 && E==0 )break;
        Read();
        Proc();
    }

    return 0;
}
